package org.dtrd.modules.archive.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.dtrd.config.system.Constant;
import org.dtrd.modules.archive.entity.bean.lifeway.*;
import org.dtrd.modules.archive.entity.bean.report.LifewayReport;
import org.dtrd.modules.archive.entity.po.DtrdEntRdArchiveLifeway;
import org.dtrd.modules.archive.mapper.DtrdEntRdLifewayMapper;
import org.dtrd.modules.archive.service.IDtrdEntRdLifewayService;
import org.dtrd.modules.patient.entity.bean.report.LifewayReportBean;
import org.dtrd.modules.system.service.ISysDictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.StringJoiner;
import java.util.stream.Collectors;

/**
@author 萧
@date 2021-10-01 22:47:27
**/
@Service
@DS("multi-datasource1")
public class DtrdEntRdLifewayServiceImpl extends ServiceImpl<DtrdEntRdLifewayMapper, DtrdEntRdArchiveLifeway> implements IDtrdEntRdLifewayService {

    @Autowired
    private ISysDictService dictService;

    private final static String pubSchemeExerciseMode = "pubSchemeExerciseMode";

    private final static String pubSchemeExerciseFrequency = "pubSchemeExerciseFrequency";

    private final static String pubSchemeExercisePeriod = "pubSchemeExercisePeriod";

    private final static String pubSchemeExerciseTime = "pubSchemeExerciseTime";

    private final static String pubSchemeDrinkFrequency = "pubSchemeDrinkFrequency";

    private final static String sleepQuality = "sleepQuality";


    @Override
    public LifewayReport getPatientLifeWayReport(Integer patientId) {
        DtrdEntRdArchiveLifeway lifeway = getOne(Wrappers.lambdaQuery(DtrdEntRdArchiveLifeway.class)
                .eq(DtrdEntRdArchiveLifeway::getPatientId, patientId));
        return Optional.ofNullable(lifeway).map(po -> {
            LifewayReport lifewayReport = new LifewayReport();
            // 运动
            lifewayReport.setSport(new LifewayReportBean().exerciseParseFromPO(po));
            // 吸烟
            lifewayReport.setSmoke(new LifewayReportBean().smokingParseFromPO(po));
            // 饮酒
            lifewayReport.setWine(new LifewayReportBean().drinkParseFromPO(po));
            // 睡眠
            lifewayReport.setSleep(new LifewayReportBean().sleepParseFromPO(po));
            // 饮食
            lifewayReport.setDiet(new LifewayReportBean().eatParseFromPO(po));
            return lifewayReport;
        }).orElse(null);
    }

    @Override
    public List<LifewayDetail> getArchiveLifewayList(Integer patientId) {
        LambdaQueryWrapper<DtrdEntRdArchiveLifeway> queryWrapper = baseQueryWrapper()
                .eq(DtrdEntRdArchiveLifeway::getPatientId, patientId)
                .orderByDesc(DtrdEntRdArchiveLifeway::getCreateTime);
        List<DtrdEntRdArchiveLifeway> poList = list(queryWrapper);
        List<LifewayDetail> dtoList = new ArrayList<>(poList.size());
        if (!poList.isEmpty()) {
            dtoList = poList.stream().map(LifewayDetail::new).collect(Collectors.toList());
        }
        return dtoList;
    }

    @Override
    public boolean updateEatingRecord(LifewayEatingDTO dto) {
        DtrdEntRdArchiveLifeway lifeway = new DtrdEntRdArchiveLifeway();
        if(dto.getDataId() == null){
            lifeway.init();
        }
        lifeway.parseFromDto(dto);
        return this.saveOrUpdate(lifeway);
    }

    @Override
    public boolean updateDrinkingRecord(LifewayDrinkingDTO dto) {
        DtrdEntRdArchiveLifeway lifeway = new DtrdEntRdArchiveLifeway();
        if(dto.getDataId() == null){
            lifeway.init();
        }
        lifeway.parseFromDto(dto);
        return this.saveOrUpdate(lifeway);
    }

    @Override
    public boolean updateSmokingRecord(LifewaySmokingDTO dto) {
        DtrdEntRdArchiveLifeway lifeway = new DtrdEntRdArchiveLifeway();
        if(dto.getDataId() == null){
            lifeway.init();
        }
        lifeway.parseFromDto(dto);
        return this.saveOrUpdate(lifeway);
    }

    @Override
    public boolean updateSleepingRecord(LifewaySleepDTO dto) {
        DtrdEntRdArchiveLifeway lifeway = new DtrdEntRdArchiveLifeway();
        if(dto.getDataId() == null){
            lifeway.init();
        }
        lifeway.parseFromDto(dto);
        return this.saveOrUpdate(lifeway);
    }

    @Override
    public boolean updateExerciseRecord(LifewayExerciseDTO dto) {
        DtrdEntRdArchiveLifeway lifeway = new DtrdEntRdArchiveLifeway();
        if(dto.getDataId() == null){
            lifeway.init();
        }
        lifeway.parseFromDto(dto);
        return this.saveOrUpdate(lifeway);
    }

    private LambdaQueryWrapper<DtrdEntRdArchiveLifeway> baseQueryWrapper() {
        return Wrappers.lambdaQuery(DtrdEntRdArchiveLifeway.class)
                .eq(DtrdEntRdArchiveLifeway::getIsDel, 0);
    }

    private LambdaUpdateWrapper<DtrdEntRdArchiveLifeway> baseUpdateWrapper() {
        return Wrappers.lambdaUpdate(DtrdEntRdArchiveLifeway.class)
                .eq(DtrdEntRdArchiveLifeway::getIsDel, 0);
    }
}
